package day40.edu;

import org.junit.Test;
import org.junit.runner.Computer;

import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;

// Created: 10/19/2025, ChangJiang
public class StringTest {
    @Test
    public void test1() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class clazz = Class.forName("java.lang.String");
        String str = (String)clazz.newInstance();

        Field valueField = clazz.getDeclaredField("value");
        valueField.setAccessible(true);
        System.out.println(valueField.get(str));
    }
    
    @Test
    public void test2()
    {
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("Do you study english and programming?");
            }
        };
        r1.run();

        Runnable r2 = () -> {
            System.out.println("Do you keep in mind");
        };
        r2.run();

        System.out.println("Yeah, i do it");
    }
    
    @Test
    public void test3()
    {
        Comparator<Integer> c1 = new Comparator<Integer>()
        {
            @Override
            public int compare(Integer o1, Integer o2)
            {
                return Integer.compare(o1, o2);
            }
        };

        Comparator<Integer> c2 = (o1, o2) -> Integer.compare(o1, o2);


        int compar1 = c1.compare(12, 21);
        System.out.println(compar1);
    }
    
    @Test
    public void test4()
    {
        Consumer<String> con1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con1.accept("Hello");

        // lambda expression
        Consumer<String> con2 = s -> System.out.println(s);
        con2.accept("World");

        PrintStream ps = System.out;
        Consumer<String> con3 = ps::println;
        con3.accept("Hello");
    }
    
    @Test
    public void test5()
    {
        Comparator<String> com1 = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {

                return o1.compareTo(o2);
            }
        };
        System.out.println(com1.compare("abc", "afd"));

        Comparator<String> com2 = (o1, o2) -> o1.compareTo(o2);

        Comparator<String> com3 = String :: compareTo;
        System.out.println(com3.compare("hello", "World"));
    }

    @Test
    public void test6()
    {
        BiPredicate<String, String> bipr1 = new BiPredicate<String, String>() {
            @Override
            public boolean test(String s, String s2) {
                return s.equals(s2);
            }
        };
        System.out.println(bipr1.test("abc", "adc"));

        // lambda expression
        BiPredicate<String,String> bipr2 = String :: equals;;
        System.out.println(bipr2.test("abc", "abc"));
    }


}
